﻿<%@ Page Title="" Language="C#" MasterPageFile="~/LayoutCenter.Master" AutoEventWireup="true" CodeBehind="TestPlanManagement.aspx.cs" Inherits="RapidWebDev.Web.TestCenter.Aspx.TestPlanManagement" %>
<asp:Content ID="Content1" ContentPlaceHolderID="Main" runat="server">
 <asp:ScriptManager ID="ScriptManagerObj" EnablePartialRendering="true" EnableScriptGlobalization="true"
        runat="server" />

    <script type="text/javascript">


        function loadRemoteData() {
            var TestProjectHierarchyId = $("#<%=TestProjectHierarchyId.ClientID%>").val();
//
            var postdata = { condition: { parentId: TestProjectHierarchyId, testCenterHierarchyType: '1'} };
var post = JSON.stringify(postdata);
$.ajax({
                url: '/Services/TestCenterService.svc/json/GetChildrenTreeNode',
                type: 'POST',
                contentType: "application/json",
                timeout: 10000,
                dataType: "json",
                data: post,
                timeout: 10000,
                success: onLoadRemoteDataSuccessfully,
                failure: onLoadRemoteDataFailed
                 });
                
//            Ext.Ajax.request(
//    {
//        url: '/Services/TestCenterService.svc/json/GetChildrenTreeNode',
//        data: '{ "condition": { "parentId":'+ TestProjectHierarchyId +', "testCenterHierarchyType": "1"} }',  
//        method: "POST",
//        processData: true,                
//        contentType: "application/json",
//        dataType: "json", 
//        success: onLoadRemoteDataSuccessfully,
//        failure: onLoadRemoteDataFailed
//    });
        }

        function onLoadRemoteDataFailed(response, options)
        {
            alert(response.responseText);
        }

//        function isTheNodeValueSelected(id)
//        {
//            if (id == null) return false;
//            for (var i = 0; i < _cachedSelection.length; i++)
//                if (_cachedSelection[i].Id.toLowerCase() == id.toLowerCase()) return true;

//            return false;
//        }

        function convertHierarchyDataToExtTreeRootNode(hierarchyDataArray)
        {
            var treeNodes = new Array();
//            if (hierarchyDataArray.length == 0)
//            {
//                //create a default category for this user
//                var topcategory = { hierarchyDataObject: { HierarchyType: 'category', Name: 'Top Category'} };
//                var toppostData = JSON.stringify(topcategory);

//                $.ajax({
//                    url: '/Services/HierarchyService.svc/json/Save',
//                    data: toppostData,
//                    type: "POST",
//                    processData: true,
//                    contentType: "application/json",
//                    timeout: 10000,
//                    dataType: "json",  
//                    success: function(res)
//                    {
//                        var hierarchyDataArray = Ext.decode(res.responseText);
//                        var treeNode = { text: hierarchyDataArray[0].Name, id: hierarchyDataArray[0].HierarchyDataId, editable: false, expanded: true };
//                        return { children: treeNodes };
//                        
//                    },

//                    error: function(xhr)
//                    {
//                        alert(xhr.responseText);
//                        return;
//                    }

//                });
//               
//            }
//            else
//            {
                for (var i = 0; i < hierarchyDataArray.length; i++)
                {
                    var hierarchyData = hierarchyDataArray[i];
                    var nodeParentValue = "";
                    eval("nodeParentValue=hierarchyData.ParentHierarchyDataId");

                    if (nodeParentValue == null || nodeParentValue == "")
                    {
                        var nodeText = "";
                        var nodeValue = "";
                        eval("nodeText=hierarchyData.Name");
                        eval("nodeValue=hierarchyData.HierarchyDataId");
                        //var isChecked = isTheNodeValueSelected(nodeValue);
                        var treeNode = { text: nodeText, id: nodeValue, editable: false, expanded: true };
                        convertHierarchyDataToExtTreeDataSource(treeNode, hierarchyDataArray);

                        if (treeNode.children == undefined || treeNode.children == null)
                            treeNode.leaf = true;
                        else
                            treeNode.leaf = false;

                        treeNodes.push(treeNode);
                    }
               // }

                return { children: treeNodes };
            }
        }

        function convertHierarchyDataToExtTreeDataSource(parentTreeNode, hierarchyDataArray)
        {
            if (hierarchyDataArray.length == 0) return;
        
            for (var i = 0; i < hierarchyDataArray.length; i++)
            {
                var hierarchyData = hierarchyDataArray[i];
                var nodeParentValue = "";
                eval("nodeParentValue=hierarchyData.ParentHierarchyDataId");
                if (nodeParentValue != parentTreeNode.id) continue;

                if (parentTreeNode.children == undefined || parentTreeNode.children == null)
                    parentTreeNode.children = new Array();

                var nodeText = "";
                var nodeValue = "";
                eval("nodeText=hierarchyData.Name");
                eval("nodeValue=hierarchyData.HierarchyDataId");

                //var isChecked = isTheNodeValueSelected(nodeValue);
                var treeNode = { text: nodeText, id: nodeValue, editable: false, expanded: true };
                parentTreeNode.children.push(treeNode);

                convertHierarchyDataToExtTreeDataSource(treeNode, hierarchyDataArray);
                if (treeNode.children == undefined || treeNode.children == null)
                    treeNode.leaf = true;
                else
                    treeNode.leaf = false;
            }
        }

        function onLoadRemoteDataSuccessfully(response, options) {
            var hierarchyDataArray = response.GetChildrenTreeNodeResult;

            var tree = new Ext.tree.TreePanel(
        {
            id: "TestProject",
            el: 'tree',
            region: 'west',
            useArrows: false,
            autoScroll: true,
            animate: false,
            animCollapse: false,
            enableDD: false,
            rootVisible: false,
            frame: false,
            forceLayout: true,
            title: "TestProject",
            root: convertHierarchyDataToExtTreeRootNode(hierarchyDataArray)

        });

            tree.render();
            tree.root.expand(true);
            tree.show();

            function ontreeclick(node, e)
            {
                $("#TestCaseManagement").attr("src", "/TestCaseManagement/DynamicPage.svc?suite=" + node.id);               
            }
            tree.on('click', ontreeclick);

            //Ext.lib.Ajax.defaultPostHeader = 'application/json';
            var yourHeaders = { 'Content-Type': 'application/json' };
            Ext.apply(Ext.Ajax.defaultHeaders, yourHeaders);
            //Ext.Ajax.defaultHeaders = false;
            //Ext.Ajax.defaultHeaders = { 'Content-Type': 'application/json' };

            var rightClick = new Ext.menu.Menu(
        {
            id: 'rightClickCont',
            items: [{
                id: 'rMenu1',
                text: 'New TestSuite',
                handler: function()
                {
                    Ext.Msg.prompt('Name', 'Please enter Test Suite name:', function(btn, text)
                    {
                        if (btn == 'ok')
                        {

                            var selectNode = tree.getSelectionModel().getSelectedNode();

                            var testsuite = { post: { Summary: 'testsuite', Name: text, ParentId: selectNode.id} };
                            var postData = JSON.stringify(testsuite);

                            $.ajax({
                                url: '/Services/TestCenterService.svc/json/SaveSuite',
                                data: postData,
                                type: "POST",
                                processData: true,
                                contentType: "application/json",
                                timeout: 10000,
                                dataType: "json",
                                success: function(res)
                                {
                                    var retId = res.SaveJsonResult;
                                    //tree reload
                                    var retNode = { text: text, id: retId, editable: false, expanded: true };
                                    selectNode.appendChild(retNode);
                                },

                                error: function(res)
                                {
                                    alert(res.responseText);
                                    return;
                                }
                            });
                        }
                    });
                }
            },
            { id: 'rMenu2',
                text: 'Edit'
            },
            { id: 'rMenu3',
                text: 'Delete',
                handler: function() //call the service to delete it
                {
                    Ext.Msg.alert('Delete', 'Are you sure to delete this suite', function(btn)
                    {
                        if (btn == 'ok')
                        {
                            var selectNode = tree.getSelectionModel().getSelectedNode();
                            var parentNode = selectNode.parentNode;

                            $.ajax({
                                url: '/Services/HierarchyService.svc/json/HardDeleteHierarchyData/' + selectNode.id,
                                type: 'GET',
                                contentType: "application/json",
                                timeout: 10000,
                                dataType: "json",
                                success: function(res)
                                {
                                    parentNode.removeChild(selectNode, false);
                                },

                                error: function(res)
                                {
                                    alert(res.responseText);
                                    return;
                                }
                            });
                        }
                    });
                }
            }
            ]


        });

            tree.on('contextmenu', function(node, event)
            {

                event.preventDefault();
                node.select();
                rightClick.showAt(event.getXY());
            });


            var navigationItem = "~/TestCaseManagement/DynamicPage.svc?suite=" + tree.root.firstChild.id;
            

            var iFramePageUrl = navigationItem + (navigationItem.indexOf('?') > -1 ? '&' : '?') + 'stamp=' + (new Date()).format('YmdHisu');
            //var iFramePageSignatureUrl = navigationItem2 + (navigationItem2.indexOf('?') > -1 ? '&' : '?') + 'stamp=' + (new Date()).format('YmdHisu');
            var openingTabPanel = new Ext.TabPanel(
				{
				    id: 'openingTabPanel',
				    region: 'center',
				    deferredRender: true,
				    activeTab: 0,
				    margins: '0 0 0 0',
				    stateful: false
				});

				var tabPanelHtml = '<iframe id="TestCaseManagement" tabid="0" src="' + iFramePageUrl + '" frameborder="0" width="100%" class="tabInnerIFrame" scrolling="no"></iframe>';
				//var tabPanelSignatureHtml = '<iframe id="SignatureManagement" tabid="1" src="' + iFramePageSignatureUrl + '" frameborder="0" width="100%" class="tabInnerIFrame" scrolling="no"></iframe>';
            
            var createdComponent = openingTabPanel.add(
			{
			    id: '0',
			    region: 'center',
			    title: 'TestCaseManagement',
			    closable: false,
			    autoScroll: false,
			    header: false,
			    html: tabPanelHtml,
			    listeners:
				{
				    resize: function(component, adjWidth, adjHeight, rawWidth, rawHeight)
				    {
				        if (component == undefined) return;

				        if (rawHeight != undefined)
				        {
				            $("#TestCaseManagement").height(rawHeight);
				        }
				        else
				        {
				            var height = component.getHeight();
				            $("#TestCaseManagement").height(height);
				        }

				        $("#TestCaseManagement").width("100%");
				    },
				    beforedestroy: function(component)
				    {
				        var iframeElement = Ext.getDom("TestCaseManagement");
				        if (iframeElement)
				        {
				            var tabIdToRemove = component.getId();
				            openedNavigationItemIdArray = openedNavigationItemIdArray.remove(tabIdToRemove);
				            if (Ext.isIE) iframeElement.src = "javascript:false";
				            Ext.removeNode(iframeElement);
				            Ext.destroy(iframeElement);
				        }

				        return true;
				    }
				}
			});

			createdComponent.fireEvent("resize");

//			var createdComponent1 = openingTabPanel.add(
//			{
//			    id: '1',
//			    region: 'center',
//			    title: 'Signature Management',
//			    closable: false,
//			    autoScroll: false,
//			    header: false,
//			    html: tabPanelSignatureHtml,
//			    listeners:
//				{
//				    resize: function(component, adjWidth, adjHeight, rawWidth, rawHeight)
//				    {
//				        if (component == undefined) return;

//				        if (rawHeight != undefined)
//				        {
//				            $("#SignatureManagement").height(rawHeight);
//				        }
//				        else
//				        {
//				            var height = component.getHeight();
//				            $("#SignatureManagement").height(height);
//				        }

//				        $("#SignatureManagement").width("100%");
//				    },
//				    beforedestroy: function(component)
//				    {
//				        var iframeElement = Ext.getDom("SignatureManagement");
//				        if (iframeElement)
//				        {
//				            var tabIdToRemove = component.getId();
//				            openedNavigationItemIdArray = openedNavigationItemIdArray.remove(tabIdToRemove);
//				            if (Ext.isIE) iframeElement.src = "javascript:false";
//				            Ext.removeNode(iframeElement);
//				            Ext.destroy(iframeElement);
//				        }

//				        return true;
//				    }
//				}
//			});

//			createdComponent1.fireEvent("resize");

//            var contentPanel = new Ext.TabPanel({
//                region: 'center',
//                enableTabScroll: true,
//                activeTab: 0,
//                items: [
//                { contentEl: 'Article', title: 'Article Template Management', html: "" },
//                { contentEl: 'Signature', title: 'Signature Management' }
//            ]
//            });

            new Ext.Viewport({
                layout: 'border',
                items: [tree, openingTabPanel]

            });



        }

      

        Ext.onReady(function()
        {
            try
            {
                loadRemoteData();
            }
            catch (Error)
            {
                alert(Error);
            };
        });

    </script>

    <style type="text/css">
        #container
        {
            margin: 0 auto;
            width: 100%;
        }
        #tree
        {
            float: left;
            width: 20%;
            height: 100%;
            background: #6cf;
        }
        #content
        {
            float: right;
            width: 80%;
            height: 100%;
            background: #cff;
        }
    </style>
    <div id="tree" />
    <asp:TextBox  ID="TestProjectHierarchyId" runat="server" style="display:none"   />
</asp:Content>

